package cm.mefobe.android.telecomcost.view;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

public class CountryPickerView extends LinearLayout {

	private Context _context;
	private TextView _label;
	private CountrySpinner _countrySpinner;		

	public CountryPickerView(Context context, String label) {
		super(context);
		Log.i("CountryPickerView", "creating the CountryPickerView view for " + label);
		_context = context;
		_label.setText(label);
	}

	private void setCountryPickerView() {
		if (isInEditMode()) {
			Log.i("CountryPickerView", "isInEditMode");
		} else {
			Log.i("CountryPickerView", "adding views...");
			setLayoutParams(new LayoutParams(android.widget.LinearLayout.LayoutParams.MATCH_PARENT,android.widget.LinearLayout.LayoutParams.WRAP_CONTENT));
			setOrientation(LinearLayout.VERTICAL);
			_label = new TextView(_context);
			_countrySpinner = new CountrySpinner(_context);				

			_label.setText("Country ");									

			addView(_label);
			addView(_countrySpinner);				
		}
	}

	public CountryPickerView(Context context, AttributeSet attrs) {
		super(context, attrs);
		Log.i("CountryPickerView",
				"creating the CountryPickerView with AttributeSet");

		_context = context;		
	}

	public CountryPickerView(Context context) {
		super(context);
		Log.i("CountryPickerView", "creating the CountryPickerView");
		_context = context;
		setCountryPickerView();
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		Log.i("CountryPickerView", "onLayout");
		super.onLayout(changed, l, t, r, b);
//		getCountrySpinner().setAdapter(null);
	}

	@Override
	protected void onAttachedToWindow() {
		Log.i("CountryPickerView", "onAttachedToWindow");
		super.onAttachedToWindow();
	}

	@Override
	protected void onDraw(Canvas canvas) {
		Log.i("CountryPickerView", "onDraw");
		super.onDraw(canvas);
	}

	@Override
	protected void onFinishInflate() {
		Log.i("CountryPickerView", "onFinishInflate");
		super.onFinishInflate();
		setCountryPickerView();
	}

	@Override
	public void updateViewLayout(View view,
			android.view.ViewGroup.LayoutParams params) {
		Log.i("CountryPickerView", "updateViewLayout");
		super.updateViewLayout(view, params);
	}

	public void setLabel(String label) {
		if (_label != null) {
			_label.setText(label);
		} else {
			Log.i("CountryPickerView", "cannot set the label...textView is null");
		}
	}

	public CountrySpinner getCountrySpinner() {		
		return _countrySpinner;
	}
}
